<?php
/**
 * Created by PhpStorm.
 * User: Digvijay
 * Date: 8/6/14
 * Time: 10:28 PM
 */

class Websitestats extends MY_Controller{
    private $ga_email = 'websiteapps.api@gmail.com';
    private $ga_password = 'vjcmfmahdqkckgbh';
    private $ga_profile_id = '99722807';

    function __construct(){
        parent::__construct();
        date_default_timezone_set('Asia/Kolkata');
    }

    function index() {
        $visits = 0;
        $bounce = 0;
        $pageViews = 0;
        $error = "";
        $worldMap = "";
        try {
            $this->load->library('Gapi', array('email' => $this->ga_email, 'password' => $this->ga_password));
            $currDate = date("Y-m-d", time());
            $startDate = "2013-01-01";
            $this->gapi->requestReportData($this->ga_profile_id, array('visitorType'), array('visits', 'bounces', 'pageviews'), null, null, $startDate, $currDate);
            foreach ($this->gapi->getResults() as $result) {
                $visits = $visits + $result->getVisits();
                $bounce = $bounce + $result->getBounces();
                $pageViews = $pageViews + $result->getPageViews();
            }

            $currDate = null;
            $startDate = null;
            if ($currDate == null) {
                $currDate = date("Y-m-d", time());
            }
            if ($startDate == null) {
                $startDate = "2013-01-01";
            }

            $this->gapi->requestReportData($this->ga_profile_id, array('country'), array('visits'), null, null, $startDate, $currDate);

            $worldMap = "";
            foreach ($this->gapi->getResults() as $result) {
                $worldMap .= "['" . $result->getCountry() . "'," . $result->getVisits() . "],";
            }
            $worldMap = substr($worldMap, 0, -1);
            $worldMap = "[['Country','Visits']," . $worldMap ."]";

        } catch (Exception $e) {
            $error = "Failed To Calculate Website Statistics, Please try again.";
        }


        $data = array("pageTitle"=> "Website Statistics", "visits" => $visits, "bounce" => $bounce, "pageViews" => $pageViews, "error" => $error, "worldMap"=>$worldMap);
        $this->load->view("admin/websitestats", $data);
    }

    function worldmap($currDate = null, $startDate = null) {
        if (!$this->input->is_ajax_request()) {
            show_404();
            return;
        }
        $response = new Response();
        try{
            $this->load->library('Gapi', array('email' => $this->ga_email, 'password' => $this->ga_password));

            if ($currDate == null) {
                $currDate = date("Y-m-d", time());
            }
            if ($startDate == null) {
                $startDate = "2013-01-01";
            }

            $this->gapi->requestReportData($this->ga_profile_id, array('country'), array('visits'), null, null, $startDate, $currDate);

            $arrayData = array();
            foreach ($this->gapi->getResults() as $result) {
                $info = array("country" => $result->getCountry(), "visitors" =>$result->getVisits() );
                array_push($arrayData, $info);
            }
            $response->setData($arrayData);
        }catch(Exception $e){
            $response->setError(true);
            $response->setErrorMessage($e->getMessage());
        }
        $this->output->set_content_type('application/json');
        echo json_encode($response);
    }

    function pie() {
        if (!$this->input->is_ajax_request()) {
            show_404();
            return;
        }
        $response = new Response();
        try {
            $this->load->library('Gapi', array('email' => $this->ga_email, 'password' => $this->ga_password));
            $curr_date = date("Y-m-d", time());
            $startDate = "2014-01-01";
            $this->load->library('Gapi', array('email' => $this->ga_email, 'password' => $this->ga_password));
            $this->gapi->requestReportData($this->ga_profile_id, array('visitorType'), array('visitors'), null, null, $startDate, $curr_date);
            $pie = array();
            foreach ($this->gapi->getResults() as $result) {
                array_push($pie,array($result->getVisitorType()=>$result->getVisitors()));
            }
            $response->setData($pie);
        } catch (Exception $e) {
            $response->setError(true);
            $response->setErrorMessage($e->getMessage());
        }
        $this->output->set_content_type('application/json');
        echo json_encode($response);
    }
}


/**
 * try {
 * date_default_timezone_set('Asia/Kolkata');
 * $ga_email = 'websiteapps.api@gmail.com';
 * $ga_password = 'vjcmfmahdqkckgbh';
 * $ga_profile_id = '72222103';
 * $curr_date = date("Y-m-d", time());
 * $startDate = "2013-01-01";
 * $lastYear = strtotime(date("Y",time())."-01-01");
 * $this->load->library('Gapi', array('email' => $ga_email, 'password' => $ga_password));
 * $this->gapi->requestReportData($ga_profile_id, array('visitorType'), array('visitors'), null, null, $startDate, $curr_date);
 * $pie = "";
 * foreach ($this->gapi->getResults() as $result) {
 * $pie .= "['" . $result->getVisitorType() . "'," . $result->getVisitors() . "],";
 * }
 * $pie = substr($pie, 0, -1);
 *
 * $this->gapi->requestReportData($ga_profile_id, array('country'), array('visits'), null, null, $startDate, $curr_date);
 * $worldMap = "";
 * foreach ($this->gapi->getResults() as $result) {
 * $worldMap .= "['" . $result->getCountry() . "'," . $result->getVisits() . "],";
 * }
 * $worldMap = "['Country','Visits']," . $worldMap;
 * $worldMap = substr($worldMap, 0, -1);
 *
 * $this->gapi->requestReportData($ga_profile_id, array('visitorType'), array('visits', 'bounces', 'pageviews'), null, null, $startDate, $curr_date);
 * $visits = 0;
 * $bounce = 0;
 * $pageViews = 0;
 * foreach ($this->gapi->getResults() as $result) {
 * $visits = $visits + $result->getVisits();
 * $bounce = $bounce + $result->getBounces();
 * $pageViews = $pageViews + $result->getPageViews();
 * }
 * $table = array("visits" => $visits, "bounce" => $bounce, "pageViews" => $pageViews);
 *
 * $this->gapi->requestReportData($ga_profile_id, array('month'), array('visits'), null, null, date('Y-m-d', $lastYear), $curr_date);
 * $months = array('01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'April', '05' => 'May', '06' => 'June', '07' => 'July', '08' => 'Aug', '09' => 'Sept', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
 * $arr = array();
 * foreach ($this->gapi->getResults() as $result) {
 * $arr[$result->getMonth()] = $result->getVisits();
 * }
 * ksort($arr);
 * $timeline = "";
 * foreach ($arr as $key => $val) {
 * $timeline .= "['" . $months[$key] . "'," . $val . "],";
 * }
 * $timeline = "['Month','Visits']," . $timeline;
 * $timeline = substr($timeline, 0, -1);
 *
 * $this->gapi->requestReportData($ga_profile_id, array('city'), array('visits'), null, null, $startDate, $curr_date);
 * $cityStats = "";
 * foreach ($this->gapi->getResults() as $result) {
 * $cityStats .= "['" . $result->getCity() . "'," . $result->getVisits() . "],";
 * }
 * $cityStats = substr($cityStats, 0, -1);
 * $data = array("pie" => $pie, "worldMap" => $worldMap, "table" => $table, "timeLine" => $timeline, "cityStats" => $cityStats);
 * $this->load->view('admin/dashbord', $data);
 * } catch (Exception $e) {
 * $data = array("error" => true);
 * $this->load->view('admin/dashbord', $data);
 * } **/